<template>
	<view class="container box">
		<uni-search-bar class="kg-search" :focus="true" v-model="searchValue">
		</uni-search-bar>
		<scroll-view scroll-y="true" class="kgs">
			<template v-for='item in kgData'>
				<view class="kg-item">
					<image :src="item.img" class="item-left" mode="aspectFill"></image>
					<view class="item-right">
						<view class="right-title">{{item.title}}</view>
						<view class="right-introduce text-overflow">{{item.info}}</view>
						<view class="btn" @click="detailFn(item.id)">
							详情
						</view>
					</view>
				</view>
			</template>
		</scroll-view>

	</view>
</template>

<script>
	import {
		getKGData
	} from '../../../database/index.js'
	export default {
		data() {
			return {
				searchValue: '',
				kgDatas: '',
				kgData: ''
			}
		},
		async onReady() {
			this.kgDatas = await getKGData()
			this.kgData = await getKGData()
		},
		async onPullDownRefresh() {
			this.kgDatas = await getKGData()
			this.kgData = await getKGData()
		},
		computed: {
			searchVal: async function() {
				let keTemp = []
				if (!this.searchValue) {
					keTemp = await getKGData()
				} else {
					for (let kg of this.kgDatas) {
						if (kg.title.includes(this.searchValue)) {
							keTemp.push(kg)
						}
					}
				}
				this.kgData = keTemp
				return keTemp
			}
		},
		methods: {
			detailFn(id) {
				uni.navigateTo({
					url: `/pages/index/kg/kgDetail/kgDetail?id=${id}`
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	$item-height: 240rpx;

	.box {
		display: flex;
		flex-direction: column;

		.kgs {
			background-color: #f4f4fa;
			box-sizing: border-box;
			padding: 0 20rpx;
			flex: 1;
			height: 1px;
			padding-top: 10rpx;

			.kg-item {
				display: flex;
				margin-bottom: 25rpx;

				.item-left {
					border-radius: 0 15rpx 0;
					width: 200rpx;
					height: 260rpx;
				}

				.item-right {
					margin-top: 20rpx;
					padding: 0 15rpx;
					padding-top: 20rpx;
					box-sizing: border-box;
					background-color: white;
					display: inline-block;
					flex: 1;
					position: relative;
					height: $item-height;
					color: black;

					.right-title {
						font-weight: bold;
						letter-spacing: 1rpx;
					}

					.right-introduce {
						margin-top: 10rpx;
						text-overflow: ellipsis;
						font-size: 0.8rem;
						font-weight: 600;
						color: #7f7f7f;
					}



					.btn {
						position: absolute;
						right: 15rpx;
						bottom: 20rpx;
						width: 100rpx;
						border-radius: 10rpx;
						min-height: 0;
						font-size: 0.8rem;
						padding: 10rpx;
						background-color: #113f6b;
						text-align: center;
						color: white;
					}


				}
			}
		}
	}

	.kg-search {
		padding: 0 10rpx;
		background-color: white;
		border-bottom: 1px solid rgba(119, 136, 153, .2);
	}
</style>